{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (3382753527.py, line 7)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[3], line 7\u001b[1;36m\u001b[0m\n\u001b[1;33m    form PIL import ImageGrab\u001b[0m\n\u001b[1;37m         ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import tkinter\n",
    "import tkinter.simpledialog\n",
    "import tkinter.colorchooser\n",
    "import tkinter.filedialog\n",
    "#需要执行pip install pillow\n",
    "form PIL import ImageGrab\n",
    "\n",
    "root=tkinter.TK()\n",
    "root.title('MyPaint----by Dong Fuguo')\n",
    "root['width']=800\n",
    "root['height']=600\n",
    "\n",
    "#控制是否允许画图的变量，1：允许，0：不允许\n",
    "canDraw=tkinter.IntVar(value=1)\n",
    "#控制画图类型的变量，1：曲线，2：直线，3：矩形，4：文本，5：橡皮\n",
    "what=tkinter.IntVar(value=1)\n",
    "#记录鼠标位置的变量\n",
    "X=tkinter.IntVar(value=0)\n",
    "Y=tkinter.IntVar(value)\n",
    "#前景色，背景色\n",
    "foreColor='#000000'\n",
    "backColor='#FFFFFF'\n",
    "#创建画布\n",
    "image=tkinter.Photolmage()\n",
    "canvas=tkinter.Canvas(root,bg='white',width=800,height=600)\n",
    "canvas.create_image(800,600,image=image)\n",
    "#鼠标左键单击，允许画图，并记录当前鼠标位置\n",
    "def onLeftButtonDown(event):\n",
    "    canDraw.set(1)\n",
    "    X.set(event.x)\n",
    "    Y.set(event.y)\n",
    "    if what.get()==4:\n",
    "        canvas.create_text(event.x,event.y,text=text)\n",
    "canvas.bind('<Button-1>',onLeftButtonDown)\n",
    "#记录最后绘制图形的id\n",
    "lastDraw=0\n",
    "#按住鼠标左键移动，画图\n",
    "def onLeftButtonMove(event):\n",
    "    global lastDraw\n",
    "    if canDraw.get()==0:\n",
    "        return\n",
    "    if what.get()==2:\n",
    "#使用当前选择的前景色绘制曲线\n",
    "       canvas.create_line(X.get(),Y.get(),event.x,event.y,fill=foreColor)\n",
    "       X.set(event.x)\n",
    "       Y.set(event.y)\n",
    "    elif what.get()==2:\n",
    "\n",
    "#绘制直线，先删除刚刚画过的直线，再画一条新的直线\n",
    "        try:\n",
    "            canvas.delete(lastDraw)\n",
    "        except Exception as e:\n",
    "            pass\n",
    "        lastDraw=canvas.create_line(X.get(),Y.get(),event.x,event.y,fill=backColor,outline=foreColor)\n",
    "    elif what.get()==5:\n",
    "#橡皮，使用背景色填充10*10的矩形区域\n",
    "         canvas.create_rectangle(event.x-5,event.y-5,event.x+5,event.y+5,outline=backColor,fill=backColor)\n",
    "         canvas.bind('<B1-Motion>',onLeftButtonMove)\n",
    "#鼠标左键抬起，不允许再画图了\n",
    "         def onLeftButtonUp(event):\n",
    "             if what.get()==2:\n",
    "                 canvas.create_line(X.get(),Y.get(),event,x,event.y,fill=foreColor)\n",
    "    elif what.get()==3:\n",
    "        canvas.create_rectangle(X.get(),Y.get(),event.x,event.y,fill=backColor,outline=foreColor)\n",
    "        canDraw.set(0)\n",
    "        global lastDraw\n",
    "#防止切换图形时误删上次绘制的图形\n",
    "        lastDraw=0\n",
    "    canvas.bind('<ButtonRelease-1>',onLeftButtonUp)\n",
    "#创建菜单\n",
    "    menu=tkinter.Menu(root,tearoff=0)\n",
    "#打开图像文件\n",
    "    def Open():\n",
    "        filename=tkinter.filedialog.askopenfilename(title='Open Image',filetypes=[('image','*.jpg*.png*.gif')])\n",
    "        if filename:\n",
    "            global image\n",
    "            image=tkinter.Photolmage(file=filename)\n",
    "            canvas.create_image(80,80,image=image)\n",
    "        menu.add_command(label='Open',command=Open)\n",
    "        def Save():\n",
    "            left=int(root.winfo_rootx())\n",
    "            top=int(root.winfo_rooty())\n",
    "            width=root.winfo_width()\n",
    "            height=root.winfo_height()\n",
    "            im=ImageGrab.grab((left,top,left+width,top+heigth))\n",
    "            filename=tkinter.filedialog.asksaveasfilename(title='保存图片',fileypes=[('图片文件','*.png')])\n",
    "            if not filename:\n",
    "                return\n",
    "            if not filename.endswith('.png'):\n",
    "               filename=filename+'.png'\n",
    "            im.save(filename)\n",
    "            menu.add_command(label='Save',command=Save)\n",
    "            #添加菜单，清除，遍历画布上的所有图片，逐个删除\n",
    "            def Clear():\n",
    "                for item in canvas.find_all():\n",
    "                    canvas.delete(item)\n",
    "                menu.add_command(label='Clear',command=Clear)\n",
    "    #添加分割线\n",
    "                menu.add_separator()\n",
    "                #创建子菜单，用来选择绘图类型\n",
    "                menuType=tkinter.Menu(menu,tearoff=0)\n",
    "                def drawCurve():\n",
    "                    what.set(1)\n",
    "                    menuType.add_command(label='Curve',command=drawCurve)\n",
    "                def drawLine():\n",
    "                    what.set(2)\n",
    "                    menuType.add_command(label='Line',command=drawLine)\n",
    "                def drawRectangle():\n",
    "                    what.set(3)\n",
    "                    menuType.add_command(label='Rectangle',command=drawRectangle)\n",
    "                def drawText():\n",
    "                    global text\n",
    "                    text=tkinter.simpledialog.askstring('Input what you want to draw',prompt='')\n",
    "                    what.set(4)\n",
    "                menuType.add_command(label='Text',command=drawText)\n",
    "                menuType.add_separator()\n",
    "            #选择背景色\n",
    "                def chooseBackColor():\n",
    "                    global backColor\n",
    "                    backColor=tkinter.colorchooser.askcolor()[1]\n",
    "                menuType.add_command(label='Choose Background Color',command=chooseBackColor)\n",
    "                #橡皮\n",
    "                def onErase():\n",
    "                    what.set(5)\n",
    "                menuType.add_command(label='Erase',command=onErase)\n",
    "                menu.add_cascade(label='Type',menu=menuType)\n",
    "                #鼠标右键抬起，弹出菜单\n",
    "                def onRightButtonUp(event):\n",
    "                    menu.post(event.x_root,event.y_root)\n",
    "                canvas.bind('<ButtonRelease-3>',onRightButtonUp)\n",
    "                canvas.pack(fill=tkinter.BOTH,expand=tkinter.YES)\n",
    "                #启动应用程序\n",
    "                root.mainloop()\n",
    "               "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
